package deng.jboss.examples;

import java.net.URL;

import org.jboss.kernel.plugins.bootstrap.standalone.StandaloneBootstrap;

/**
 * The StandaloneBootstrap will only loads jboss-beans.xml in classpath,
 * and store up the arguments input. This class will extends it and use
 * the args as input to any resource filenames within classpath. Note
 * jboss-beans.xml will still be loaded first if exists. Use this class
 * like this: 
 * <pre>	
 *		new ClasspathXmlDeploymentBootstrap(new String[] { "my-jboss-beans.xml" }).run();
 * </pre>		 
 * 
 * Note: The base class StandaloneBootstrap forced subclass to wrap try/catch
 * block in usage above, because the constructor throws checked exception.
 *
 * @author dengz1
 *
 */
public class ClasspathXmlDeploymentBootstrap extends StandaloneBootstrap {
	
	/** Required constructor from parent. */
	public ClasspathXmlDeploymentBootstrap(String[] args) throws Exception {
		super(args);
	}
	
	/** Deploy all args as user specific deployments. */
	@Override
	public void run() {
		super.run(); // Let the parent init and deploy jboss-beans.xml if exists.
		
		String[] resources = args; // treats it as res names to other deployments
		ClassLoader cl = Thread.currentThread().getContextClassLoader();
		for (String res : resources) {
			log.trace("Finding classpath url for res " + res);
			URL resUrl = cl.getResource(res);
			if (resUrl == null) {
				throw new RuntimeException("Classpath resource not found: " + res);
			}
			try {
				log.debug("Deploying " + resUrl);
				deploy(resUrl);
			} catch (Throwable e) {
				throw new RuntimeException("Failed to deploy " + resUrl, e);
			}
		}
		
	}
	
	/** Provide a main entry for this class. */
	public static void main(String[] args) throws Exception {
		new ClasspathXmlDeploymentBootstrap(args).run();
	}

}
